package com.chen.controller;


import com.chen.annotation.OptLog;
import com.chen.common.OptTypeConst;
import com.chen.common.Result;
import com.chen.entity.Exam;
import com.chen.entity.Sign;
import com.chen.exception.ServiceException;
import com.chen.mapper.SignMapper;
import com.chen.service.impl.SignServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author personcoder007
 * @since 2022-09-17
 */
@RestController
@RequestMapping("/sign")
@Api(value = "SignController", tags = "报名管理")
public class SignController {

    @Autowired
    private SignServiceImpl signService;

    @Autowired
    private SignMapper signMapper;


    @ApiOperation("分页")
    @GetMapping("/findSignPage")
    public Result findSignPage(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(signService.findSignPage(pageNumber, pageSize));
    }


    @PostMapping("/delBathSign/bath")
    @ApiOperation(value = "批量删除")
    @OptLog(optType = OptTypeConst.REMOVE_BATH)
    public Result delBathSign(@RequestBody List<Integer> ids) {
        Integer integer = signMapper.delBathSign(ids);
        if (integer >= 1) {
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败");
        }
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result delBathSign(@PathVariable("id") Integer id) {
        signService.removeById(id);
        return Result.success();

    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或者更新")
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    public Result addDict(@RequestBody Sign sign) {

        sign.setTime(new Date());
        try {
            signService.saveOrUpdate(sign);
        } catch (DuplicateKeyException e) {

            throw new ServiceException(401, "已报名，请不要重复操作");
        }


        return Result.success();
    }
}

